package server

import (
	"fmt"
	"github.com/shirou/gopsutil/v3/cpu"
	"github.com/shirou/gopsutil/v3/disk"
	"github.com/shirou/gopsutil/v3/host"
	"github.com/shirou/gopsutil/v3/mem"
	"main/api"
	"main/dto"
	"sync"
	"time"
)

// Global variables
var (
	dataMutex           sync.RWMutex
	monitorInfoResponse = dto.GetMonitorInfoResponse{
		Data: dto.MonitorInfoData{
			HostInfo: dto.HostInfoData{},
			FinanceInfo: dto.FinanceInfoData{
				Metal:   make(map[string]dto.FinanceData),
				Crypto:  make(map[string]dto.FinanceData),
				Stock:   make(map[string]dto.FinanceData),
				Fund:    make(map[string]dto.FinanceData),
				Bond:    make(map[string]dto.FinanceData),
				Futures: make(map[string]dto.FinanceData),
			},
			FinanceCommentInfo: dto.FinanceCommentInfoData{
				Metal:   make(map[string][]dto.RealTimeCommentData),
				Crypto:  make(map[string][]dto.RealTimeCommentData),
				Stock:   make(map[string][]dto.RealTimeCommentData),
				Fund:    make(map[string][]dto.RealTimeCommentData),
				Bond:    make(map[string][]dto.RealTimeCommentData),
				Futures: make(map[string][]dto.RealTimeCommentData),
			},
			HotSearcheInfo: dto.HotSearcheData{
				Baidu:    make([]string, 0),
				Weibo:    make([]string, 0),
				Bilibili: make([]string, 0),
			},
		},
	}
)

func init() {
}

func formatUptime(uptime uint64) string {
	days := uptime / (24 * 3600)           // 计算天数
	hours := (uptime % (24 * 3600)) / 3600 // 计算小时
	minutes := (uptime % 3600) / 60        // 计算分钟
	seconds := uptime % 60                 // 计算秒数

	return fmt.Sprintf("%d天%d时%d分%d秒", days, hours, minutes, seconds)
}

// 更新主机资源数据
func UpdateHostData() {
	// 获取主机资源数据并更新
	dataMutex.Lock()
	defer dataMutex.Unlock()

	cpuPercent, _ := cpu.Percent(time.Second, false)
	vmStat, _ := mem.VirtualMemory()
	diskStat, _ := disk.Usage("/")
	hostStat, _ := host.Info()

	// 格式化后赋值
	monitorInfoResponse.Data.HostInfo = dto.HostInfoData{
		CPUUsage:    fmt.Sprintf("%.2f%%", cpuPercent[0]),
		MemoryUsage: fmt.Sprintf("%.2f GB", float64(vmStat.Used)/1e9),
		TotalMemory: fmt.Sprintf("%.2f GB", float64(vmStat.Total)/1e9),
		DiskUsage:   fmt.Sprintf("%.2f GB", float64(diskStat.Used)/1e9),
		TotalDisk:   fmt.Sprintf("%.2f GB", float64(diskStat.Total)/1e9),
		Uptime:      formatUptime(hostStat.Uptime),
	}
}

func UpdateMetalData() {

	// 黄金
	var goldData = api.FetchGoldData()

	// 获取金融数据并更新
	dataMutex.Lock()
	defer dataMutex.Unlock()
	monitorInfoResponse.Data.FinanceInfo.Metal["gold"] = goldData
	monitorInfoResponse.Data.FinanceInfo.Stock["000852"] = goldData

	var commentData = []dto.RealTimeCommentData{
		{
			User:       "user123",
			Province:   "北京",
			City:       "北京市",
			Comment:    "有资金护盘了",
			CreateTime: time.Now().Unix(),
		},
		{
			User:       "investor456",
			Province:   "上海",
			City:       "上海市",
			Comment:    "国家队上班了中证1000和中证500，别被骗了筹码，都是低位血筹啊",
			CreateTime: time.Now().Unix(),
		},
	}
	monitorInfoResponse.Data.FinanceCommentInfo.Stock["000852"] = commentData

	var hotSearch = []string{
		"拜登公开讲话痛斥特朗普",
		"浙商霸气回应700万美元订单被叫停",
		"关税阴影下投资者正“逃离”美国",
	}
	monitorInfoResponse.Data.HotSearcheInfo.Baidu = hotSearch
}

func UpdateCryptoData() {

	dataMutex.Lock()
	defer dataMutex.Unlock()
}
func UpdateStockData() {

	dataMutex.Lock()
	defer dataMutex.Unlock()
}
func UpdateFundData() {

	dataMutex.Lock()
	defer dataMutex.Unlock()
}
func UpdateBondData() {

	dataMutex.Lock()
	defer dataMutex.Unlock()
}
func UpdateFuturesData() {

	dataMutex.Lock()
	defer dataMutex.Unlock()
}

// 获取最新主机资源数据
func GetHostData() interface{} {
	dataMutex.RLock()
	defer dataMutex.RUnlock()
	return monitorInfoResponse
}
